#include <allegro.h>
#include <stdio.h>
#include "deplacementM.h"

#define NB_IMAGES 3



//Background


BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
	if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);
		return (NULL);
	}
	return (Background);

}



void draw_background(BITMAP *Background,BITMAP*Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}



void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}




//MB
 

void load_MB(MB *mini)
{
	int i;
	char buf[50];
	for (i = 0; i < NB_IMAGES; i++) {
		sprintf(buf, "../images/right%d.bmp", i);
		mini->img_right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "../images/left%d.bmp", i);
		mini->img_left[i] = load_bitmap(buf, NULL);
	}
	
mini->img_face=load_bitmap("../images/face.bmp", NULL);

}



void destroy_MB(MB *mini)
{
	int i;
	for (i = 0; i < NB_IMAGES; i++) 
	{
		destroy_bitmap(mini->img_right[i]);
		destroy_bitmap(mini->img_left[i]);
	}

}



void init_MB(MB *mini)
{
	mini->direction = 0;
	mini->x = 0;
	mini->y =600;
}



void move_MB_right(MB *mini,int *compteur,BITMAP * Background)
{ 

	if ( (mini->x) < (SCREEN_W - mini->img_right[0]->w) && (mini->direction)==0 )
	 {
		*compteur+=1;
		(mini->x)+=1;
		usleep(4000);
		mini->direction=0;
	}

		if((mini->x) == SCREEN_W - mini->img_right[0]->w -5)
		{mini->direction = 1;} //changer vers gauche
}



void move_MB_left(MB *mini, int *compteur, BITMAP *Background)
{

	if  (mini->x > 0 + mini->img_left[1]->w && mini->direction==1) 
	{

	*compteur+=1; // si le compteur = 10 000 il sera réinitialisé à 0
	(mini->x)-=1;
	usleep(4000);
	mini->direction=1;

	}

	if  ( (mini->x) == ( 0 + mini->img_left[1]->w))
	{mini->direction = 0;} // changer vers la droite
}



void move_MB(MB *mini,int *compteur,BITMAP * Background)
{ 

	move_MB_right(mini, compteur, Background);

	move_MB_left(mini, compteur, Background);

}



void draw_MB(MB mini,BITMAP *Buffer, int compteur)
{
	static int last_direction;

	if (mini.direction == 0)//bouger à droite
	{ 
		draw_sprite(Buffer, mini.img_right[compteur % (NB_IMAGES-1) ],mini.x, mini.y);
		last_direction=0;
	}

	if(mini.direction == 1) //bouger à gauche
	{
		draw_sprite(Buffer, mini.img_left[compteur % (NB_IMAGES-1) ],mini.x, mini.y);
		last_direction=1;
	}

}

